<template>
    <div>
        <div class="head">
            首页 &nbsp;/ &nbsp;用户管理 &nbsp;/
            <span>添加</span>
        </div>
        <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
        >
            <el-form-item label="登录账号" prop="account">
                <el-input v-model="ruleForm.account"></el-input>
            </el-form-item>
            <el-form-item label="登录密码" prop="password">
                <el-input v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="用户姓名" prop="realname">
                <el-input v-model="ruleForm.realname"></el-input>
            </el-form-item>
            <el-form-item label="手机号码" prop="telephone">
                <el-input v-model="ruleForm.telephone"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const {  mapActions } = createNamespacedHelpers("usersModules");
export default {
    data() {
        return {
            ruleForm: {
                account: '',
                password: '',
                realname: '',
                telephone: '',
              

            },
            rules: {
                account: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
                ],
                realname: [
                    { required: true, message: '请输入用户真实名字', trigger: 'blur' },
                    { min: 2, max: 5, message: '长度在 2 到 5 个字', trigger: 'blur' }
                ],
                telephone: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度在11位数字', trigger: 'blur' }
                ],
            }
        }

    },
    methods: {
        ...mapActions(['asyncAddAdmin']),
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.asyncAddAdmin({
                        account:this.ruleForm.account,
                        password:this.ruleForm.password,
                        realname:this.ruleForm.realname,
                        telephone:this.ruleForm.telephone,
                    })
                    alert('添加成功');

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style scoped lang="scss">
.head {
    width: 1250px;
    height: 80px;
    border-bottom: 1px solid #ccc;
    text-align: left;
    line-height: 70px;
    font-size: 20px;
    color: #a4a4a4;
    span {
        color: black;
        font-weight: 600;
    }
}
</style>